added some development tools
[windows-sources.git] / developer / Samples / NET Standard / ParallelExtensionsExtras / Extensions / TaskFactoryExtensions / TaskFactoryExtensions_FromAsync.cs
blobc825df22b0587d1a0fdc297648a917e3ac537e65
1 //--------------------------------------------------------------------------
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // File: TaskFactoryExtensions_FromAsync.cs
6 //
7 //--------------------------------------------------------------------------
9 namespace System.Threading.Tasks
11 /// <summary>Extensions for TaskFactory.</summary>
12 public static partial class TaskFactoryExtensions
14 /// <summary>Creates a Task that will be completed when the specified WaitHandle is signaled.</summary>
15 /// <param name="factory">The target factory.</param>
16 /// <param name="waitHandle">The WaitHandle.</param>
17 /// <returns>The created Task.</returns>
18 public static Task FromAsync(this TaskFactory factory, WaitHandle waitHandle)
20 if (factory == null) throw new ArgumentNullException("factory");
21 if (waitHandle == null) throw new ArgumentNullException("waitHandle");
23 var tcs = new TaskCompletionSource<object>();
24 var rwh = ThreadPool.RegisterWaitForSingleObject(waitHandle, delegate { tcs.TrySetResult(null); }, null, -1, true);
25 var t = tcs.Task;
26 t.ContinueWith(_ => rwh.Unregister(null), TaskContinuationOptions.ExecuteSynchronously);
27 return t;